layui.define(['layer', 'toastr'], function(exports) {
	var $ = layui.jquery;

	get_push();

	function get_push() {
		var d = new Date().getTime();
		$.ajax({
			type : "POST",
			url : push_server + '&' + d,
			dataType : "json",
			success : function(ret) {
				if (ret.status) {
					$msg = $.parseJSON(ret.data.data);
					push_info($msg);
					get_push();
				}
			}
		});
	}

	function push_info($msg) {
		var position;
		if ($msg.action.length) {
			$title = '<h3>[' + $msg.type + '] [' + $msg.action + ']</h3>';
		} else {
			$title = '<h3>[' + $msg.type + ']</h3>';
		}
		$content = '<p class="push-title">' + $msg.title + '</p>' + $msg.content;

		if (is_mobile()) {
			position = "toast-top-full-width";
		} else {
			position = "toast-bottom-right";
		}
		toastr.options = {
			"closeButton" : true,
			"positionClass" : position,
			"timeOut" : ws_push_time * 1000000
		};
		toastr.info($content, $title);
	}

	var WEB_SOCKET_SWF_LOCATION = "Public/layui/swf/WebSocketMain.swf";
	var WEB_SOCKET_DEBUG = true;
	var WEB_SOCKET_SUPPRESS_CROSS_DOMAIN_SWF_ERROR = true;

	var laychat = {
		appName : '消息',
		initUrl : './init.php', // 初始化好友列表和群组接口，这个接口返回个json，格式见
		sendMessageUrl : './send_message.php', // 发消息接口
		membersUrl : './members.php',
		uploadImageUrl : './upload_img.php',
		uploadFileUrl : './upload_file.php',
		chatLogUrl : './chat_log.php',
		bindUrl : './bind.php',
		address : 'ws://' + document.domain + ':8282',
		setMin : false,
		findUrl : false,
		userInfo : null,
		inited : false,
		socket : null,
		jq : null,
		open : function() {
			if (this.isIE6or7())
				return;
			//if(!this.jq) this.jq = jQuery.noConflict();
			if (!this.jq)
				this.jq = layui.jquery;
			this.connectWorkerman();
		},
		isIE6or7 : function() {
			var b = document.createElement('b');
			b.innerHTML = '<!--[if IE 5]><i></i><![endif]--><!--[if IE 6]><i></i><![endif]--><!--[if IE 7]><i></i><![endif]-->';
			return b.getElementsByTagName('i').length === 1;
		},
		connectWorkerman : function() {
			laychat.socket = new WebSocket(laychat.address);
			laychat.socket.onopen = function() {
				laychat.socket.send(JSON.stringify({
					type : 'init'
				}));
			};

			laychat.socket.onmessage = function(e) {
				var msg = JSON.parse(e.data);
				switch(msg.message_type) {
				// 初始化im
				case 'init':

					laychat.jq.post(laychat.bindUrl, {
						client_id : msg.client_id
					}, function(data) {
						if (data.code == 0) {
							if (enable_chat) {
								laychat.initIM(data.unread_message);
							}
						} else {
							alert('laychat服务端返回失败：' + data.msg);
						}
					}, 'json');

					return;
				// 添加一个用户到好友列表
				case 'addList':
					if (laychat.jq('#layim-friend' + msg.data.id).length == 0 && laychat.userInfo && laychat.userInfo['id'] != msg.data.id) {
						return layui.layim.addList(msg.data);
					}
					laychat.changeOnlineStatus(msg.data.id, 'online');
					return;
				// 收到一个消息
				case 'chatMessage':
					get_push();
					console.log(msg);
					if (msg.data.type == 'push') {
						get_push();
						return;
					}

					if (msg.data.type == 'group') {
						if (msg.data.from_id != laychat.userInfo.id) {
							layui.layim.getMessage(msg.data);
						}
					} else if (laychat.userInfo.id != msg.data.id) {
						layui.layim.getMessage(msg.data);
					}
					return;
				// 退出
				case 'logout':
				// 隐身
				case 'hide':
				// 上线
				case 'online':
					var status = msg.message_type;
					laychat.changeOnlineStatus(msg.id, status);
					return;
				}
			};
			laychat.socket.onclose = laychat.connectWorkerman;
		},
		sendHeartbeat : function() {
			if (this.socket && this.socket.readyState == 1) {
				this.socket.send(JSON.stringify({
					type : 'ping'
				}));
			}
		},
		changeOnlineStatus : function(id, status) {
			if (status == 'hide' || status == 'logout') {
				return laychat.jq('#layim-friend' + id + ' img').css({
					"-webkit-filter" : "grayscale(100%)",
					"-moz-filter" : "grayscale(100%)",
					"-ms-filter" : "grayscale(100%)",
					"-o-filter" : "grayscale(100%)",
					"filter" : "grayscale(100%)",
					"filter" : "gray"
				});
			}
			laychat.jq('#layim-friend' + id + ' img').removeAttr('style');
		},
		initIM : function(unread_message) {
			if (this.inited) {
				// 离线消息
				for (var key in unread_message) {
					layui.layim.getMessage(JSON.parse(unread_message[key]));
				}
				return;
			}
			this.inited = true;
			setInterval('laychat.sendHeartbeat()', 12000);
			layui.use('layim', function(layim) {
				layim.config({
					//初始化接口
					init : {
						url : laychat.initUrl
					}
					//查看群员接口
					,
					members : {
						url : laychat.membersUrl
					}
					// 上传图片
					,
					uploadImage : {
						url : laychat.uploadImageUrl
					}
					// 上传文件
					,
					uploadFile : {
						url : laychat.uploadFileUrl
					}
					//聊天记录地址
					,
					chatLog : laychat.chatLogUrl,
					find : laychat.findUrl,
					copyright : true//是否授权
					,
					title : laychat.appName,
					min : laychat.setMin,
					msgbox : laychat.msgbox //消息盒子页面地址，若不开启，剔除该项即可
				});

				//监听发送消息
				layim.on('sendMessage', function(data) {
					guid = data.mine.guid;
					$("#layui-layim-chat .layim-chat-text[guid='" + guid + "']").addClass('read');
					laychat.jq.post(laychat.sendMessageUrl, {
						data : data
					}, function(data) {
						if (data.code != 0) {
							layui.layer.msg(data.msg, {
								time : 7000
							});
						}
					}, 'json');
				});

				//监听在线状态的切换事件
				layim.on('online', function(data) {
					laychat.socket.send(JSON.stringify({
						type : data
					}));
				});

				//layim建立就绪
				layim.on('ready', function(res) {
					laychat.userInfo = layim.cache().mine;
					// 离线消息
					for (var key in unread_message) {
						layim.getMessage(JSON.parse(unread_message[key]));
					}
					// 将不在线的置为下线
					var friend_list = res.friend[0].list;
					for (var key in friend_list) {
						var user_id = friend_list[key].id;
						laychat.changeOnlineStatus(user_id, friend_list[key]['status']);
					}
				});
			});
		}
	};

	web_socket_url = 'Public/layui/js/web_socket.js';
	load_js(web_socket_url, function() {
		laychat.address = 'ws://' + document.domain + ':8282';
		// 置服务端websocket地址
		//laychat.bindUrl = './chat/bind.php';
		// 将当前socket链接与用户绑定，并加入到对应群组（群组id可以自定）
		//laychat.initUrl = './chat/init.php';
		// 初始化好友列表和群组接口，这个接口返回个json，格式见 http://www.layui.com/doc/modules/layim.html#init
		//laychat.sendMessageUrl = './chat/send_message.php';
		// 发消息接口地址
		//laychat.membersUrl = './chat/members.php';
		// 获取群成员接口地址
		laychat.uploadImageUrl = './chat/upload_img.php';
		// 上传图片接口地址
		laychat.uploadFileUrl = './chat/upload_file.php';
		// 上传文件接口地址
		//laychat.chatLogUrl = './chat/chat_log.php';
		// 消息记录地址
		laychat.setMin = true;
		//最小化

		laychat.msgbox = msg_box_url;

		// 置服务端websocket地址
		laychat.bindUrl = chat_bind_url;
		laychat.initUrl = chat_init_url;
		laychat.sendMessageUrl = chat_send_message_url;
		laychat.membersUrl = chat_member_url;
		laychat.chatLogUrl = chat_log_url;

		// 打开laychat对话框
		laychat.open();
		console.log('web_socket_url load finish');
	});
	window.laychat = laychat;
	exports('x', {});
});

